package com.ruoyi.activiti.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.activiti.mapper.AttendMapper;
import com.ruoyi.activiti.domain.Attend;
import com.ruoyi.activiti.service.IAttendService;
import com.ruoyi.common.core.text.Convert;

/**
 * 员工基本考勤信息Service业务层处理
 * 
 * @author lifan
 * @date 2021-04-08
 */
@Service
public class AttendServiceImpl implements IAttendService 
{
    @Autowired
    private AttendMapper attendMapper;

    /**
     * 查询员工基本考勤信息
     * 
     * @param id 员工基本考勤信息ID
     * @return 员工基本考勤信息
     */
    @Override
    public Attend selectAttendById(Long id)
    {
        return attendMapper.selectAttendById(id);
    }

    /**
     * 查询员工基本考勤信息列表
     * 
     * @param attend 员工基本考勤信息
     * @return 员工基本考勤信息
     */
    @Override
    public List<Attend> selectAttendList(Attend attend)
    {
        return attendMapper.selectAttendList(attend);
    }

    /**
     * 新增员工基本考勤信息
     * 
     * @param attend 员工基本考勤信息
     * @return 结果
     */
    @Override
    public int insertAttend(Attend attend)
    {
        attend.setCreateTime(DateUtils.getNowDate());
        return attendMapper.insertAttend(attend);
    }

    /**
     * 修改员工基本考勤信息
     * 
     * @param attend 员工基本考勤信息
     * @return 结果
     */
    @Override
    public int updateAttend(Attend attend)
    {
//        attend.setUpdateBy(operName);
        attend.setUpdateTime(DateUtils.getNowDate());
        return attendMapper.updateAttend(attend);
    }

    /**
     * 删除员工基本考勤信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAttendByIds(String ids)
    {
        return attendMapper.deleteAttendByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除员工基本考勤信息信息
     * 
     * @param id 员工基本考勤信息ID
     * @return 结果
     */
    @Override
    public int deleteAttendById(Long id)
    {
        return attendMapper.deleteAttendById(id);
    }


    /**
     * 导入用户数据
     *
     * @param studentList 用户数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    private static final Logger log = LoggerFactory.getLogger(AttendServiceImpl.class);

    @Override
    public String importAttend(List<Attend> attendList, Boolean updateSupport, String operName) {
        if (StringUtils.isNull(attendList) || attendList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Attend attend : attendList)
        {
            try
            {
                // 验证是否存在这个用户
                Attend u = attendMapper.selectByEmployeeName(attend.getEmployeeName() );
//                System.out.print(student.getShift()+"");
                if (StringUtils.isNull(u))
                {
                    attend.setEmployeeName(attend.getEmployeeName());
                    this.insertAttend(attend);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "考勤信息" + attend.getEmployeeName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    attend.setUpdateBy(operName);
                    this.updateAttend(attend);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "考勤信息 " + attend.getEmployeeName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "考勤信息" + attend.getEmployeeName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "考勤信息" + attend.getEmployeeName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
